layui.define(['jquery','ax'], function (exports) {

    var $ = layui.$;
    var $ax = layui.ax;

    var $ZTreeRight = function (id, url) {
        this.id = id;
        this.url = url;
        this.zTree = null;
        this.onClick = null;
        this.settings = null;
    }

    $ZTreeRight.prototype = {

        initSetting : function () {
            var settings = {
                view: {
                    dblClickExpand : false,
                    showIcon : false,
                    addDiyDom : this.addDiyDom
                },
                check: {
                    enable: false
                },
                callback: {
                    onClick : this.onClick,
                    onRightClick:this.OnRightClick
                }
            };
            return settings;
        },

        setSettings : function (val) {
            this.settings = val;
        },
        
        init : function () {
            var zNodeSeting = null;
            if(this.settings != null){
                zNodeSeting = this.settings;
            }else{
                zNodeSeting = this.initSetting();
            }
            var zNodes = this.loadNodes();
            $.fn.zTree.init($("#" + this.id), zNodeSeting, zNodes);
        },

        bindOnClick : function (func) {
            this.onClick = func;
        },

        /**
         * 加载节点
         */
        loadNodes : function() {
            var zNodes = null;
            var ajax = new $ax(Lu.ctxPath + this.url, function(data) {
                zNodes = data;
            }, function(data) {
                Lu.error("加载ztree信息失败!");
            });
            ajax.start();
            return zNodes;
        },

        /**
         * 获取选中的值
         */
        getSelectedVal : function(){
            var zTree = $.fn.zTree.getZTreeObj(this.id);
            var nodes = zTree.getSelectedNodes();
            return nodes[0].name;
        },

        addDiyDom : function(treeId, treeNode) {
            var spaceWidth = 5;
            var switchObj = $("#" + treeNode.tId + "_switch"),
                icoObj = $("#" + treeNode.tId + "_ico");
            switchObj.remove();
            icoObj.before(switchObj);

            if (treeNode.level > 1) {
                var spaceStr = "<span style='display: inline-block;width:" + (spaceWidth * treeNode.level) + "px'></span>";
                switchObj.before(spaceStr);
            }
        },

        OnRightClick : function(event, treeId, treeNode) {
            this.zTree = $.fn.zTree.getZTreeObj(treeId);
            sessionStorage.setItem("ztreeRightObjId",treeNode.id);
            if (treeNode && treeNode.noR) {
                this.zTree.cancelSelectedNode();
                showRMenu("root", event.clientX, event.clientY);
            } else {
                this.zTree.selectNode(treeNode);
                showRMenu("no", event.clientX, event.clientY);
            }
        },
         hideRMenu : function() {
            var rMenu = $("#rMenu");
            if (rMenu) rMenu.css({"visibility": "hidden"});
            $("body").unbind("mousedown", this.onBodyMouseDown);
         },
    };

    function showRMenu(type,x,y) {
        var rMenu = $("#rMenu");
        $("#rMenu ul").show();
        if (type=="root") {
            $("#m_add").show();
            $("#m_c_add").show();
            $("#m_edit").show();
            $("#m_del").show();
        } else {
            $("#m_add").hide();
            $("#m_c_add").hide();
        }

        y += document.body.scrollTop - 36;
        x += document.body.scrollLeft + 30;

        rMenu.css({"top":y+"px", "left":x+"px", "visibility":"visible"});

        $("body").bind("mousedown", onBodyMouseDown);
    };

    function onBodyMouseDown(event){
        var rMenu = $("#rMenu");
        if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {
            rMenu.css({"visibility" : "hidden"});
        }
    }



    exports('ztreeRight' , $ZTreeRight);

});